/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import _entity.OrdersDetailsInfo;
import _entity.OrdersDetailsPKInfo;
import _entity.OrdersInfo;
import entity.Orders;
import entity.OrdersDetails;
import entity.OrdersDetailsPK;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;

/**
 *
 * @author HP
 */
@Stateless
public class OrdersBean implements OrdersBeanRemote {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @PersistenceUnit(unitName = "_asmPU")
    private EntityManagerFactory emf;

    private EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    @Override
    public List<OrdersInfo> findAll() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(Orders.class));
        List<Orders> list = getEntityManager().createQuery(cq).getResultList();
        List<OrdersInfo> result = new ArrayList<OrdersInfo>();

        for (Orders c : list) {
            OrdersInfo item = new OrdersInfo();

            item.setId(c.getId());
            item.setIsCompleted(c.getIsCompleted());
            item.setIsEnable(c.getIsEnable());
            item.setIsTransported(c.getIsTransported());
            item.setOrdersDate(c.getOrdersDate());
            item.setTotalAmount(c.getTotalAmount());

            List<OrdersDetailsInfo> infoList = new ArrayList<OrdersDetailsInfo>();
            for (OrdersDetails d : c.getOrdersDetailsList()) {
                OrdersDetailsInfo info = new OrdersDetailsInfo();

                info.setCurrentPrice(d.getCurrentPrice());
                info.setQuantity(d.getQuantity());
                info.setOrders(item);

                OrdersDetailsPKInfo pk = new OrdersDetailsPKInfo();
                pk.setOrdersID(d.getOrdersDetailsPK().getOrdersID());
                pk.setProductID(d.getOrdersDetailsPK().getProductID());

                info.setOrdersDetailsPK(pk);
            }

            item.setOrdersDetailsList(infoList);

            //

            result.add(item);
        }

        return result;
    }

    @Override
    public List<OrdersInfo> findRange(int[] range) {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(Orders.class));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        List<Orders> list = q.getResultList();
        List<OrdersInfo> result = new ArrayList<OrdersInfo>();

        for (Orders c : list) {
            OrdersInfo item = new OrdersInfo();

            item.setId(c.getId());
            item.setIsCompleted(c.getIsCompleted());
            item.setIsEnable(c.getIsEnable());
            item.setIsTransported(c.getIsTransported());
            item.setOrdersDate(c.getOrdersDate());
            item.setTotalAmount(c.getTotalAmount());

            List<OrdersDetailsInfo> infoList = new ArrayList<OrdersDetailsInfo>();
            for (OrdersDetails d : c.getOrdersDetailsList()) {
                OrdersDetailsInfo info = new OrdersDetailsInfo();

                info.setCurrentPrice(d.getCurrentPrice());
                info.setQuantity(d.getQuantity());
                info.setOrders(item);

                OrdersDetailsPKInfo pk = new OrdersDetailsPKInfo();
                pk.setOrdersID(d.getOrdersDetailsPK().getOrdersID());
                pk.setProductID(d.getOrdersDetailsPK().getProductID());

                info.setOrdersDetailsPK(pk);
            }

            item.setOrdersDetailsList(infoList);

            //

            result.add(item);
        }

        return result;
    }

    @Override
    public int count() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        javax.persistence.criteria.Root<Orders> rt = cq.from(Orders.class);
        cq.select(getEntityManager().getCriteriaBuilder().count(rt));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }

    @Override
    public OrdersInfo find(Object id) {
        Orders c = getEntityManager().find(Orders.class, id);
        if (c != null) {
            OrdersInfo item = new OrdersInfo();

            item.setId(c.getId());
            item.setIsCompleted(c.getIsCompleted());
            item.setIsEnable(c.getIsEnable());
            item.setIsTransported(c.getIsTransported());
            item.setOrdersDate(c.getOrdersDate());
            item.setTotalAmount(c.getTotalAmount());

            List<OrdersDetailsInfo> infoList = new ArrayList<OrdersDetailsInfo>();
            for (OrdersDetails d : c.getOrdersDetailsList()) {
                OrdersDetailsInfo info = new OrdersDetailsInfo();

                info.setCurrentPrice(d.getCurrentPrice());
                info.setQuantity(d.getQuantity());
                info.setOrders(item);

                OrdersDetailsPKInfo pk = new OrdersDetailsPKInfo();
                pk.setOrdersID(d.getOrdersDetailsPK().getOrdersID());
                pk.setProductID(d.getOrdersDetailsPK().getProductID());

                info.setOrdersDetailsPK(pk);
            }

            item.setOrdersDetailsList(infoList);

            return item;
        }
        return null;
    }

    @Override
    public void update(OrdersInfo role) {
        Orders item = new Orders();

        List<OrdersDetails> pList = new ArrayList<OrdersDetails>();
        for (OrdersDetailsInfo p : role.getOrdersDetailsList()) {
            OrdersDetails pItem = new OrdersDetails();
            pItem.setCurrentPrice(p.getCurrentPrice());
            pItem.setQuantity(p.getQuantity());
            pItem.setOrders(item);

            OrdersDetailsPK pk = new OrdersDetailsPK();
            pk.setOrdersID(p.getOrdersDetailsPK().getOrdersID());
            pk.setProductID(p.getOrdersDetailsPK().getProductID());

            pItem.setOrdersDetailsPK(pk);

            pList.add(pItem);
        }

        item.setId(role.getId());
        item.setIsCompleted(role.isIsCompleted());
        item.setIsEnable(role.isIsEnable());
        item.setIsTransported(role.isIsTransported());
        item.setOrdersDate(role.getOrdersDate());
        item.setTotalAmount(role.getTotalAmount());
        item.setOrdersDetailsList(pList);

        getEntityManager().merge(item);
    }
}
